﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class PromjenaPassworda : Form
    {
        String JMBG, tip;
        MySqlConnection conn;
        MySqlDataReader rdr = null;
        public PromjenaPassworda(String maticni, MySqlConnection c, String t)
        {
            InitializeComponent();
            JMBG = maticni;
            conn = c;
            tip = t;
        }

        private void textBox3_Validating(object sender, CancelEventArgs e)
        {
            if (textBox2.Text != textBox3.Text)
            {
                e.Cancel = true;
                errorProvider1.SetError(textBox3, "Morate dva puta unijeti isti password");
            }
            else
                errorProvider1.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
                MessageBox.Show("Niste popunili sva polja!", "Upozorenje", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Osoba WHERE JMBG = '" + JMBG + "';", conn);
            try
            {
                rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    if (textBox1.Text != Convert.ToString(rdr["Password"]))
                        errorProvider1.SetError(textBox1, "Niste unijeli ispravan stari password");
                    else
                    {
                        errorProvider1.Clear();
                        rdr.Close();
                         MySqlCommand cmd1;
                         if (tip == "1")
                             cmd1 = new MySqlCommand("UPDATE Ucenik SET Password= '" + textBox2.Text + "' WHERE JMBG = '" + JMBG + "';", conn);      
                         else if (tip == "2")
                             cmd1 = new MySqlCommand("UPDATE Roditelj SET Password= '" + textBox2.Text + "' WHERE JMBG = '" + JMBG + "';", conn);
                         else
                             cmd1 = new MySqlCommand("UPDATE Nastavnik SET Password= '" + textBox2.Text + "' WHERE JMBG = '" + JMBG + "';", conn);
                        cmd1.ExecuteNonQuery();
                             MessageBox.Show("Password je uspjesno promjenjen", "Obavijest", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }       
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            var rez = MessageBox.Show("Zelite li odustati od promjene passworda?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rez == DialogResult.No) return;
            else
                this.Close();
        }
       
    }
}
